define(['common/js/service/boss-common-service-0.1'],
    function() {
        /**
         * Created by zhaoshen on 2015/1/31.
         */
        serviceModule.factory("SeatUtil", function (MaiDialog) {
            return {
                SVG_NS:'http://www.w3.org/2000/svg',
                XLINK : "http://www.w3.org/1999/xlink",
                INIT_TRANSFORM : {
                    tx: 0,
                    ty: 0,
                    rotate : 0,
                    px : 0,
                    py : 0,
                    scale: 1,
                    flipX: 1,
                    flipY: 1
                },
                INIT_TRANSFORM_STR : 'translate(0,0) rotate(0,0,0) scale(1) matrix(1 0 0 1 0 0)',

                // 数据状态
                DATA_STATE : {
                    EFFECTIVE:1,//"有效"，有变更
                    INVALID:2,//"无效",没有变更
                    DELETE_MARK:3,//"特殊删除"，被删除
                },

                //资源类型
                STATIC_RESOURCE_TYPE:{
                    NORMAL: 1,//"普通静态资源"
                    TEXT: 2,//"普通文本"
                    TEXT_BOUND: 3//"区域提示文本"
                },

                //元素CLASS
                ELEMENT_CLASS: {
                    FLOOR: "phy_floor_g",
                    FLOOR_GROUP: "physics_floor_group",
                    SEAT: "seats_circle_info",
                    SEAT_SELECT: "seats_circle_select",
                    RESOURCE: "venues_static_resource",
                    RESOURCE_SELECT: "venues_resource_select",
                    TEXT: "venues_static_resource",
                    BOUND_TEXT: "bound_text",
                    ROTATE_RECT: "rotate_rect"
                },

                //元素类型
                ELEMENT_TYPE: {
                    //楼层
                    FLOOR: 0,
                    //座位组
                    FLOOR_GROUP: 1,
                    //座位
                    SEAT: 2,
                    //静态资源
                    RESOURCE: 3,
                    //文本
                    TEXT: 4,
                    //旋转小方块
                    ROTATE_RECT: 5
                },

                encodeTransform: function(transObj) {
                    var tmpArr = [];
                    var tx = transObj.tx,
                        ty = transObj.ty,
                        rotate = transObj.rotate,
                        px = transObj.px,
                        py = transObj.py,
                        scale = transObj.scale,
                        flipX = transObj.flipX,
                        flipY = transObj.flipY;

                    if(isNaN(tx)){ tx = 0; }
                    if(isNaN(ty)){ ty = 0; }
                    if(isNaN(rotate)){ rotate = 0; }
                    if(isNaN(px)){ px = 0; }
                    if(isNaN(py)){ py = 0; }
                    if(isNaN(scale)){ scale = 1; }
                    if(isNaN(flipX)){ flipX = 1; }
                    if(isNaN(flipY)){ flipY = 1; }

                    tmpArr.push('translate(', tx, ',', ty, ') ');
                    tmpArr.push('rotate(', rotate, ',', px, ',', py, ') ');
                    tmpArr.push('scale(', scale, ') ');
                    tmpArr.push('matrix('+flipX+' 0 0 '+flipY+' 0 0)');

                    return tmpArr.join('');
                },

                decodeTransform: function(transString) {
                    if(!transString){
                        return { tx: 0, ty: 0, rotate: 0, px: 0, py: 0, scale: 1, flipX: 1, flipY: 1};
                    }

                    var tmpExp = /.*matrix.*/,tfExp,
                        match;
                    //含有matrix字段
                    if(tmpExp.test(transString)){
                        tfExp = /translate\((.*)?\)\srotate\((.*)?\)\sscale\((.*)?\)\smatrix\((.*)?\)/;
                    }else{
                        tfExp = /translate\((.*)?\)\srotate\((.*)?\)\sscale\((.*)?\)/;
                    }
                    match = tfExp.exec(transString);

                    var sTranslate = match[1],
                        sRotate = match[2],
                        sScale = match[3],
                        sMatrix = match[4];

                    var regTranslate = /(.*)?,(.*)?/,
                        regRotate = /(.*)?,(.*)?,(.*)?/,
                        regMatrix = /(.*)? 0 0 (.*)? 0 0/;

                    var iTx = 0;
                    var iTy = 0;
                    var mt;
                    if (regTranslate.test(sTranslate) ==  true) {
                        mt = regTranslate.exec(sTranslate);
                        iTx = 1 * mt[1];
                        iTy = 1 * mt[2];
                    }

                    var iPx = 0;
                    var iPy = 0;
                    var iRotate = 0;
                    if (regTranslate.test(sRotate) ==  true) {
                        mt = regRotate.exec(sRotate);
                        iRotate = 1 * mt[1];
                        iPx = 1 * mt[2];
                        iPy = 1 * mt[3];
                    }

                    var iFlipX = 1,
                        iFlipY = 1;
                    if(regMatrix.test(sMatrix)){
                        mt = regMatrix.exec(sMatrix);
                        iFlipX = 1 * mt[1];
                        iFlipY = 1 * mt[2];
                    }

                    var iScale = 1 * sScale;

                    if(isNaN(iTx)) iTx = 0;
                    if(isNaN(iTy)) iTy = 0;
                    if(isNaN(iRotate)) iRotate = 0;
                    if(isNaN(iPx)) iPx = 0;
                    if(isNaN(iPy)) iPy = 0;
                    if(isNaN(iScale)) iScale = 0;
                    if(isNaN(iFlipX)) iFlipX = 1;
                    if(isNaN(iFlipY)) iFlipY = 1;

                    return {
                        tx: iTx,
                        ty: iTy,
                        rotate: iRotate,
                        px: iPx,
                        py: iPy,
                        scale: iScale,
                        flipX: iFlipX,
                        flipY: iFlipY
                    };
                },

                showModalDialog: function(modalId){
                    $('#'+modalId).modal('show');
                },
                closeModalDialog: function(modalId){
                    $('#'+modalId).modal('hide');
                }
            }
        })
    });